package com.gj.server;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author: xiaobin
 * @create: 2020-12-18 20:55
 */
public class MyByteToGyroDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        System.out.println(byteBuf.readableBytes());

        if (byteBuf.readableBytes() > 7) {

            GyroTransferData data = new GyroTransferData();
            data.setAngleType(byteBuf.readByte());
            data.setAngleSize(byteBuf.readByte());
            data.setAngle(byteBuf.readByte());
            data.setDataType(byteBuf.readByte());
            data.setNo(byteBuf.readByte());

            byte b = byteBuf.readByte();
            if (b == (byte)0xbb) {
                list.add(data);
                // 清空buf
                byteBuf.clear();
            } else {
                // 清空buf
                byteBuf.clear();
            }
        }
    }
}
